extends Node2D

@onready var friend_list: ItemList = $CanvasLayer/JoinPanel/FriendList

#创建游戏触发面板
@onready var on_create: Button = $CanvasLayer/OnCreatePanel/VBoxContainer/OnCreate
@onready var on_load: Button = $CanvasLayer/OnCreatePanel/VBoxContainer/OnLoad
@onready var on_join: Button = $CanvasLayer/OnCreatePanel/VBoxContainer/OnJoin
#创建游戏面板
@onready var create_game_panel: Panel = $CanvasLayer/CreateGamePanel
@onready var game_name: LineEdit = $CanvasLayer/CreateGamePanel/CreateBox/GameName
@onready var game_auth: LineEdit = $CanvasLayer/CreateGamePanel/CreateBox/GameAuth
@onready var create_game: Button = $CanvasLayer/CreateGamePanel/CreateBox/CreateGame
@onready var on_create_close: TextureButton = $CanvasLayer/CreateGamePanel/OnClose
#
@onready var game_list_panel: Panel = $CanvasLayer/GameListPanel
@onready var game_list_box: ItemList = $CanvasLayer/GameListPanel/GameListBox
@onready var open_game: Button = $CanvasLayer/GameListPanel/OpenGame
@onready var on_list_close: TextureButton = $CanvasLayer/GameListPanel/OnClose
#
@onready var join_game_panel: Panel = $CanvasLayer/JoinGamePanel
@onready var game_id: LineEdit = $CanvasLayer/JoinGamePanel/GameId
@onready var join_game_auth: LineEdit = $CanvasLayer/JoinGamePanel/GameAuth
@onready var join_game: Button = $CanvasLayer/JoinGamePanel/JoinGame
@onready var on_join_close: TextureButton = $CanvasLayer/JoinGamePanel/OnClose

func _ready() -> void:
	_init_style()
	_init_signal()
	_init_game_list()
	_query_friend_list()
	_init_friend_list()

func _init_style() -> void :
	create_game_panel.hide()
	game_list_panel.hide()
	join_game_panel.hide()

func _init_signal() -> void :
	create_game.button_up.connect(_on_create_game)
	on_create.button_up.connect(_on_create)
	on_create_close.button_up.connect(_on_create)
	on_load.button_up.connect(_on_load)
	open_game.button_up.connect(_open_game)
	on_list_close.button_up.connect(_on_load)
	on_join.button_up.connect(_on_join)
	join_game.button_up.connect(_join_game)
	on_join_close.button_up.connect(_on_join)

func _on_create_game() -> void :
	var game_data = Global.current_game_data if Global.current_game_data else GameData.new({"game_name":game_name.text})
	SceneManage._save_game_data_rw("game_save",game_data.game_id,game_data._get_stuct())
	if not await SceneManage._join_match(game_data.game_id) :
		print("加入房间失败")
		return
	Global.current_game_auth = game_auth.text
	Global.current_game_data = game_data
	get_tree().call_deferred("change_scene_to_file","res://scenes/lobby.tscn")

func _on_create() -> void :
	create_game_panel.visible = not create_game_panel.visible

func _on_load() -> void :
	game_list_panel.visible = not game_list_panel.visible

func _on_join() -> void :
	join_game_panel.visible = not join_game_panel.visible

func _init_game_list() -> void :
	for child in game_list_box.get_children() :
		child.queue_free()
	var list = await SceneManage._get_game_data_list("game_save")
	for item in list :
		var value = JSON.to_native(JSON.parse_string(item.value))
		var index = game_list_box.add_item(value.game_name)
		game_list_box.set_item_metadata(index,value)

func _open_game() -> void :
	var items = game_list_box.get_selected_items()
	if not items : return
	var item_meta = game_list_box.get_item_metadata(items.get(0))
	var game_data = await SceneManage._get_game_data("game_save",item_meta.game_id)
	if not game_data :
		print("加入房间失败")
		return
	Global.current_game_data = GameData.new(game_data)
	game_name.text = Global.current_game_data.game_name
	_on_load()
	_on_create()

func _join_game() -> void :
	var game_data = {
		"game_name" : "player_data",
		"game_id" : game_id.text,
		"user_id" : SceneManage.current_player.user.id,
		"game_auth" : join_game_auth.text
	}
	if not await SceneManage._join_match(game_data.game_id) :
		print("加入房间失败")
		return
	Global.current_game_data = GameData.new(game_data)
	Global.current_game_auth = game_data.game_auth
	_on_join()
	get_tree().call_deferred("change_scene_to_file","res://scenes/lobby.tscn")

func _init_friend_list() -> void :
	var timer = Timer.new()
	timer.autostart = true
	timer.wait_time = 10.0
	timer.timeout.connect(_query_friend_list)
	call_deferred("add_child",timer)

func _query_friend_list() -> void :
	friend_list.clear()
	var f_list = await SceneManage._get_friends(0)
	for i in f_list :
		friend_list.add_item(i.user.display_name)
